package cn.shengchao.controler.knowledge;

import cn.shengchao.examstar.knowledge.application.KnowledgeApplicationService;
import cn.shengchao.examstar.knowledge.application.dto.KnowledgePointDetailDTO;
import cn.shengchao.examstar.knowledge.application.dto.KnowledgeTreeNodeDTO;
import cn.shengchao.examstar.knowledge.application.query.GetKnowledgePointDetailQuery;
import cn.shengchao.examstar.knowledge.application.query.KnowledgeTreeQuery;
import cn.shengchao.util.helper.LoginHelper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import top.continew.starter.log.annotation.Log;

import java.util.List;

/**
 * 知识体系相关控制器
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/${project.version}/knowledge")
@Log("知识体系")
public class KnowledgeController {

    private final KnowledgeTreeQuery knowledgeTreeQuery;
    private final KnowledgeApplicationService knowledgeApplicationService;

    /**
     * 获取完整知识树
     *
     * @param specificationTypeId 类目ID
     * @return 知识树
     */
    @GetMapping("/tree/full")
    public List<KnowledgeTreeNodeDTO> getFullKnowledgeTree(
            @RequestParam("specificationTypeId") Long specificationTypeId) {
        Long userId = LoginHelper.getUserId();
        log.info("网关层请求获取完整知识树: 用户ID={}, 类目ID={}", userId, specificationTypeId);
        return knowledgeTreeQuery.getFullKnowledgeTree(userId, specificationTypeId);
    }

    /**
     * 获取根节点和叶子节点知识树
     *
     * @param specificationTypeId 类目ID
     * @return 知识树（仅包含根节点和叶子节点）
     */
    @GetMapping("/tree/root-and-leaf")
    public List<KnowledgeTreeNodeDTO> getRootAndLeafKnowledgeTree(
            @RequestParam("specificationTypeId") Long specificationTypeId) {
        Long userId = LoginHelper.getUserId();
        log.info("网关层请求获取根节点和叶子节点知识树: 用户ID={}, 类目ID={}", userId, specificationTypeId);
        return knowledgeTreeQuery.getRootAndLeafKnowledgeTree(userId, specificationTypeId);
    }

    /**
     * 获取知识点详情
     *
     * @param knowledgePointId 知识点ID
     * @return 知识点详情
     */
    @GetMapping("/point/{knowledgePointId}")
    public KnowledgePointDetailDTO getKnowledgePointDetail(
            @PathVariable Long knowledgePointId) {
        Long userId = LoginHelper.getUserId();
        log.info("网关层请求获取知识点详情: 用户ID={}, 知识点ID={}", userId, knowledgePointId);
        GetKnowledgePointDetailQuery query = new GetKnowledgePointDetailQuery(knowledgePointId, userId);
        return knowledgeApplicationService.getKnowledgePointDetail(query);
    }
} 